<?php

// +----------------------------------------------------------------------
// | Apprh.Shop
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.apprh.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// +----------------------------------------------------------------------
// | Author: 雲溪荏苒 <290648237@qq.com>
// +----------------------------------------------------------------------

namespace app\common\model;

use think\Model;

/**
 * 产品模型
 * Class Product
 * @package app\common\model
 * @author 雲溪荏苒 <290648237@qq.com>
 * @date 2017/12/10
 */
class Product extends Model{

    public function getList($condition =[], $curPage = 0, $pageSize = 50, $order='sort asc', $field='*')
    {
        if (isset($condition['cid']) && $condition['cid'] == 0) {
            unset($condition['cid']);
        }
        if (!$curPage) {
            $curPage = request()->param('cur_page', 1);// 获取当前页码
        }
        $count = db('product')->field($field)->where($condition)->count();
        $result = db('product')->field($field)->where($condition)->page($curPage, $pageSize)->order($order)->select();

        return [$result, $count];
    }

    public function getOneProduct($id)
    {
        $result = db('product')->where(['status' => 1, 'id' => $id])->find();

        return $result;
    }

    /**
     * 访问量+1
     * @param $id
     * @throws \think\Exception
     */
    public static function setViewInc($id)
    {
        db('product')->where('id', $id)->setInc('view');
    }

    /**
     * @param int $classLimit
     * @param int $productLimit
     * @return mixed
     */
    public function getProductList($classLimit = 0, $productLimit = 0, $isAll = false)
    {
        // 获取全部产品
        if ($isAll) {
            $productList = db('product')->where(['status' => 1])->order('sort asc')->limit($productLimit)->select();
        } else {
            // 获取产品分类列表
            $productClassList = db('product_class')
                ->where('status', 1)
                ->order('sort asc')
                ->limit(0,$classLimit)
                ->select();
            $productList = $productClassList;
            if(!empty($productList)) {
                foreach ($productList as $key=>$item) {
                    $productList[$key]['products'] = db('product')->where(['status'=>1, 'cid'=>$item['id']])->order('sort asc')->limit(0,$productLimit)->select();
                }
            }
        }

        return $productList;
    }

}














